import { asyncRouters, constantRoutes } from '@/router'

const asyncRoutes = []

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param userType
 */
export function filterAsyncRoutes(routes, userType) {
  let res = []

  routes.forEach(route => {
    if (route.userType === userType) {
      res = route.routes
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, userType) {
    // let accessedRoutes
    return new Promise(resolve => {
      // const accessedRoutes = filterAsyncRoutes(asyncRoutes, userType)
      const accessedRoutes = asyncRouters
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
